package com.danger.Activtiy;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.ypy.eventbus.EventBus;

import net.steamcrafted.loadtoast.LoadToast;

public class MainActivity extends AppCompatActivity {

    private Button button;
    private TextView receiver_textView;
    private Button show;
    private Button error;
    private Button success;
    private Button refresh;
    private LoadToast loadToast;
    private ViewGroup root;
    private Button crop_button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        EventBus.getDefault().register(this);
        initView();
        initEvent();

    }

    private void initView() {
        button = (Button) findViewById(R.id.button);
        receiver_textView = (TextView) findViewById(R.id.receiver_textView);
        show = (Button) findViewById(R.id.show);
        error = (Button) findViewById(R.id.error);
        success = (Button) findViewById(R.id.success);
        refresh = (Button) findViewById(R.id.refresh);
        crop_button = (Button) findViewById(R.id.crop_button);
        final String text = "Sending reply...";
        loadToast = new LoadToast(this).setText(text).setTranslationY(100).show();
        root = (ViewGroup) findViewById(android.R.id.content);
        View v = new View(this);
        root.addView(v, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 400));
    }

    private void initEvent() {
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, ShowActivity.class));
            }
        });
        show.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loadToast.show();
            }
        });
        error.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loadToast.error();
            }
        });
        success.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loadToast.success();
            }
        });
        refresh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                View view = new View(MainActivity.this);
                root.addView(view, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 400));
            }
        });
        crop_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, CustomViewActivity.class));
            }
        });
    }

    public void onEventMainThread(String message) {
        receiver_textView.setText(message);
        Toast.makeText(this, message, Toast.LENGTH_LONG).show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}